package com.ktjiaoyu.server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjiaoyu.server.pojo.Admin;
import com.ktjiaoyu.server.pojo.Employee;
import com.ktjiaoyu.server.pojo.Leavebill;
import com.ktjiaoyu.server.pojo.RespBean;
import com.ktjiaoyu.server.service.ILeavebillService;
import com.ktjiaoyu.server.service.impl.LeavebillServiceImpl;
import com.ktjiaoyu.server.utils.PageUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjx
 * @since 2024-03-23
 */
@RestController
@RequestMapping("/leavebill")
public class LeavebillController {
    @Resource
    ILeavebillService leavebillService;

    @ApiOperation("显示请假单分页列表信息")
    @GetMapping("/list")
    public RespBean getLeaveBillListPage(
            @RequestParam(value = "currPageNo", defaultValue = "1") Integer currPageNo,
            @RequestParam(value = "pageSize", defaultValue = "2")Integer pageSize,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "content", required = false) String content,
            @RequestParam(value = "beginTime", required = false) String beginTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "sort")String sort) {

        Page<Leavebill> page = new Page<Leavebill>(currPageNo, pageSize);

        Admin admin=(Admin)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        IPage<Leavebill> adminIPage = leavebillService.getLeaveBillListPage(page, title, content,sort, beginTime, endTime,admin.getId() );
        // 使用Math.toIntExact将Long类型转换成int类型
        Integer total = Math.toIntExact(adminIPage.getTotal());
        Integer current = Math.toIntExact(adminIPage.getCurrent());
        PageUtil<Leavebill> pageList = new PageUtil<Leavebill>(total, current, adminIPage.getRecords());
        return RespBean.success("获取请假记录列表成功", pageList);
    }

    @PostMapping("/saveLeaveBill")
    @PreAuthorize("hasAuthority('/leavebill/saveLeaveBill')")
    @ApiOperation("新增请假单信息")
    public RespBean saveLeaveBill(@RequestBody Leavebill leavebill) {
        // 得到当前登录用户对象
        Admin admin = (Admin)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        leavebill.setUserid(admin.getId());
        if (leavebillService.save(leavebill)) {
            return RespBean.success("新增请假单信息成功!");
        } else {
            return RespBean.error("新增请假单信息失败!");
        }
    }

    // 通过请假单ID查询请假单详情信息
    @GetMapping("/getLeaveById/{id}")
    @ApiOperation("通过请假单ID查询请假单详情信息")
    public RespBean getLeaveById(@PathVariable("id")Integer id) {
        Leavebill leavebill = leavebillService.getById(id);
        return RespBean.success("查询请假单详情信息成功", leavebill);
    }

    // 保存修改后的请假单信息
    @PostMapping("/updateLeaveBill")
    @PreAuthorize("hasAuthority('/leavebill/updateLeaveBill')")
    @ApiOperation("保存修改后的请假单信息")
    public RespBean updateLeaveBill(@RequestBody Leavebill leavebill) {
        // 得到当前登录用户对象
        Admin admin = (Admin)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        leavebill.setUserid(admin.getId());
        if (leavebillService.saveOrUpdate(leavebill)) {
            return RespBean.success("修改请假单信息成功!");
        } else {
            return RespBean.error("修改请假单信息失败!");
        }
    }

    @GetMapping("/delLeaveBill/{id}")
    @PreAuthorize("hasAuthority('/leavebill/delLeaveBill')")
    @ApiOperation("删除请假单")
    public RespBean delLeaveBill(@PathVariable("id")Integer id) {
        if (leavebillService.removeById(id)) {
            return RespBean.success("删除请假单成功!");
        } else {
            return RespBean.error("删除请假单失败!");
        }
    }

    /**
     * 通过请假id集合，批量删除指定的请假单信息
     */
    @GetMapping("/batchDeleteLeaveBill")
    @ApiOperation("通过请假id集合，批量删除指定的请假单信息")
    public RespBean batchDeleteLeaveBill(@RequestParam("ids") String ids) {
        try {
            String [] leaveIds = ids.split(",");
            // 把请假单id集合进行遍历
            for (String id : leaveIds) {
                this.leavebillService.removeById(id);
            }
            return RespBean.success("批量删除请假单信息成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return RespBean.error("批量删除请假单信息失败!");
        }
    }


}
